//
//  WSRegisterViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/22.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSRegisterViewController.h"

#import "SendMsgService.h"
#import "RegistService.h"

@interface WSRegisterViewController ()

@property (weak, nonatomic) IBOutlet UIView *phoneContentView;
@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UIView *passwordView;
@property (weak, nonatomic) IBOutlet UIView *chooseIDView;

@property (weak, nonatomic) IBOutlet UIButton *teaSelectBtn;
@property (weak, nonatomic) IBOutlet UIButton *stuSelectBtn;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;
@property (weak, nonatomic) IBOutlet UIButton *msgCodeBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topMargin;

@end

@implementation WSRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)setup {
    // 设置底部边框
    [self.phoneContentView setBorder:kSeparatorColor borderWidth:1 borderType:UIBorderSideTypeBottom];
    [self.codeView setBorder:kSeparatorColor borderWidth:1 borderType:UIBorderSideTypeBottom];
    [self.passwordView setBorder:kSeparatorColor borderWidth:1 borderType:UIBorderSideTypeBottom];
    
    // 如果是忘记密码，则隐藏我的身份一栏
    self.chooseIDView.hidden = self.isForgetPwd;
    self.pwdTextField.placeholder = self.isForgetPwd ? @"请输入新密码" : @"请设置密码";
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.topMargin.constant = self.isForgetPwd ? 60.f : 120.f;
}

#pragma mark - Button Actions

// 获取短信验证码
- (IBAction)getMsgCode:(id)sender {
    if (![_phoneTextField hasText]) {
        [self showHint:@"请输入手机号"];
        return;
    }
    
    // 开时倒计时
    [self.msgCodeBtn countDownWithTime:60 withTitle:_msgCodeBtn.currentTitle andCountDownTitle:nil countDownBlock:^(id obj) {
        
    } isEnabled:NO];
    
    // 获取短信验证码
    [SendMsgService sendCode:_phoneTextField.text complete:^(BOOL success) {
        if (success) {
            [self showHint:@"发送成功"];
        }
    }];
}


// 选择身份
- (IBAction)selectRoleAction:(UIButton *)sender {
    if (sender == _teaSelectBtn) {
        if (sender.isSelected) return;
        _teaSelectBtn.selected = YES;
        _stuSelectBtn.selected = NO;
        
    } else {
        if (sender.isSelected) return;
        _stuSelectBtn.selected = YES;
        _teaSelectBtn.selected = NO;
    }
}

// 注册
- (IBAction)doRegist:(id)sender {
    if (![_phoneTextField hasText]) {
        [self showHint:@"请输入手机号"];
        return;
    }
    if (![_codeTextField hasText]) {
        [self showHint:@"请输入验证码"];
        return;
    }
    if (![_pwdTextField hasText]) {
        [self showHint:@"请设置密码"];
        return;
    }
    
    // loading
    [self showHud];
    
    BOOL isTeacher = YES;
    
    // 老师注册
    if (_teaSelectBtn.isSelected) {        
//        [RegistService registWithMobile:_phoneTextField.text password:_pwdTextField.text code:_codeTextField.text isTeacher:YES completion:^(id  _Nonnull responseData) {
//            [self hideHud];
//            KDISPATCH_DELAY(1.0, ^{
//                [self.navigationController popViewControllerAnimated:YES];
//            });
//        }];
        isTeacher = YES;
        return;
    }
    
    // 学生注册
    if (_stuSelectBtn.isSelected) {
//        ZhStaffTabBarViewController *staffTabCtrl = [[ZhStaffTabBarViewController alloc] init];
//        [self setRootViewController:staffTabCtrl];
        isTeacher = NO;
    }
    
    [RegistService registWithMobile:_phoneTextField.text password:_pwdTextField.text code:_codeTextField.text isTeacher:isTeacher completion:^(id  _Nonnull responseData) {
        [self hideHud];
        KDISPATCH_DELAY(1.0, ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    }];
}



@end
